<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      fixed
    >
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
      >搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 频道栏 -->
    <van-tabs
      class="channel-tabs"
      v-model="active"
      animated
      swipeable
    >
      <van-tab
        v-for="channel in channels"
        :key="channel.id"
        :title="channel.name"
      >
        <!-- 文章列表S -->
        <artList :channel="channel" />
        <!-- 文章列表E -->
      </van-tab>

      <template #nav-right>
        <div
          @click="isChannelEditer = true"
          class="hamburger-btn"
        > <i class="iconfont icon-gengduo"></i></div>
        <i class="placeholder"></i>
      </template>
    </van-tabs>
    <!--/ 频道栏 -->

    <!-- 频道编辑栏 -->
    <van-popup
      v-model="isChannelEditer"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <channelAditer
        :active="active"
        :mychannels="channels"
        @updateActive='onUpdateActive'
      />
    </van-popup>
    <!-- /频道编辑栏 -->

  </div>
</template>

<script>
import artList from './components/art_list.vue'
import { getUserChannel } from '@/api/user.js'
import channelAditer from './components/channel_editer.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'

export default {
  name: 'HomeIndex',
  components: {
    artList,
    channelAditer
  },
  props: {},
  data () {
    return {
      active: 0,
      channels: [],
      isChannelEditer: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadChannel()
  },
  mounted () { },
  methods: {
    async loadChannel () {
      try {
        let channels = []
        if (this.user) {
          // 已登录 请求获取用户频道列表
          const { data } = await getUserChannel()
          channels = data.data.channels
        } else {
          // 未登录 判断是否有本地的频道列表数据
          const localChannels = getItem('TOUTIAO_CHANNELS')

          // 有 拿来使用
          if (localChannels) {
            channels = localChannels
          } else {
            // 没有 请求获取默认频道列表
            const { data } = await getUserChannel()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (err) {
        this.$toast('获取频道失败')
      }
    },
    onUpdateActive (index, isChannelEditer = true) {
      this.active = index
      // 关闭弹出层
      this.isChannelEditer = isChannelEditer
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      height: 80px;
      .van-tab__text {
        font-size: 27px;
        color: #777777;
      }
    }

    .van-tab--active .van-tab__text {
      color: #333 !important;
    }
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 99;
      height: 82px;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background: rgba(50, 150, 250, 1);
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }

    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
